<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="交易编号" prop="transNo">
        <el-input
          v-model="queryParams.transNo"
          placeholder="请输入交易编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易时间" prop="transDate">
        <el-date-picker
          v-model="queryParams.transDate"
          clearable
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择交易时间"
        />
      </el-form-item>
      <el-form-item label="交易地址" prop="transAddr">
        <el-input
          v-model="queryParams.transAddr"
          placeholder="请输入交易地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易金额" prop="transAmount">
        <el-input
          v-model="queryParams.transAmount"
          placeholder="请输入交易金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易名称" prop="transName">
        <el-input
          v-model="queryParams.transName"
          placeholder="请输入交易名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户编号" prop="transCustomerNo">
        <el-input
          v-model="queryParams.transCustomerNo"
          placeholder="请输入客户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易商户编号" prop="transBusinessNo">
        <el-input
          v-model="queryParams.transBusinessNo"
          placeholder="请输入交易商户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:trans:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:trans:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:trans:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:trans:import']"
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:trans:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar
        :show-search.sync="showSearch"
        @queryTable="getList"
      />
    </el-row>

    <el-table
      v-loading="loading"
      :data="transList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="交易编号" align="center" prop="transNo" :show-overflow-tooltip="true" />
      <el-table-column
        label="交易时间"
        align="center"
        prop="transDate"
        width="180"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.transDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="交易地址" align="center" prop="transAddr" :show-overflow-tooltip="true" />
      <el-table-column label="交易金额" align="center" prop="transAmount" :show-overflow-tooltip="true" />
      <el-table-column label="交易名称" align="center" prop="transName" :show-overflow-tooltip="true" />
      <el-table-column label="客户编号" align="center" prop="transCustomerNo" :show-overflow-tooltip="true" />
      <el-table-column
        label="交易商户编号"
        align="center"
        prop="transBusinessNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="230"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:trans:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            :disable="scope.row.imgContent===''"
            @click="handleview(scope.row)"
          >查看收据</el-button>
          <el-button
            v-hasPermi="['system:trans:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCreate(scope.row)"
          >生成</el-button>
          <el-button
            v-hasPermi="['system:trans:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['system:trans:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 选择印章和模板弹窗 -->
    <el-dialog :title="title" :visible.sync="chooseShow" width="500px" append-to-body>
      <el-form ref="form" :model="chooseData" label-width="80px">
        <el-form-item label="模板">
          <el-select v-model="chooseData.templateNo" placeholder="请选择模板">
            <el-option
              v-for="item in templateList"
              :key="item.id"
              :label="item.templateName"
              :value="item.templateNo"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="印章">
          <el-select v-model="chooseData.sealNo" placeholder="请选择印章">
            <el-option
              v-for="item in sealList"
              :key="item.id"
              :label="item.sealName"
              :value="item.sealNo"
            />
          </el-select></el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCreateForm">确 定</el-button>
        <el-button @click="chooseShow = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改交易信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="交易时间" prop="transDate">
          <el-date-picker
            v-model="form.transDate"
            clearable
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择交易时间"
          />
        </el-form-item>
        <el-form-item label="交易地址" prop="transAddr">
          <el-input v-model="form.transAddr" placeholder="请输入交易地址" />
        </el-form-item>
        <el-form-item label="交易金额" prop="transAmount">
          <el-input v-model="form.transAmount" placeholder="请输入交易金额" />
        </el-form-item>
        <el-form-item label="交易名称" prop="transName">
          <el-input v-model="form.transName" placeholder="请输入交易名称" />
        </el-form-item>
        <el-form-item label="客户编号" prop="transCustomerNo">
          <el-input
            v-model="form.transCustomerNo"
            placeholder="请输入客户编号"
          />
        </el-form-item>
        <el-form-item label="交易商户编号" prop="transBusinessNo">
          <el-input
            v-model="form.transBusinessNo"
            placeholder="请输入交易商户编号"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 交易导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload" />
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div slot="tip" class="el-upload__tip text-center">
          <div slot="tip" class="el-upload__tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的交易数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTrans,
  getTrans,
  delTrans,
  addTrans,
  updateTrans,
  createTrans
} from '@/api/system/trans'
import { getToken } from '@/utils/auth'
import { listSeal } from '@/api/system/seal'
import { listTemplate } from '@/api/system/template'

export default {
  name: 'Trans',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 交易信息表格数据
      transList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        transNo: null,
        transDate: null,
        transAddr: null,
        transAmount: null,
        transName: null,
        transCustomerNo: null,
        transBusinessNo: null
      },
      // 表单参数
      form: {},
      // 交易导入参数
      upload: {
        // 是否显示弹出层（交易导入）
        open: false,
        // 弹出层标题（交易导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的交易数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/system/trans/importData'
      },
      // 表单校验
      rules: {},
      chooseShow: false,
      sealList: [],
      templateList: [],
      chooseData: {
        templateNo: '',
        sealNo: ''
      },
      transRow: {}
    }
  },
  created() {
    this.getList()
    this.getSealList()
    this.getTemplateList()
  },
  methods: {
    // 查看收据
    handleview(row) {
      // 打包url http://47.109.156.247:8080
      const url = 'http://47.109.156.247:8090' + row.imgContent
      window.open(url, '_blank')
    },
    // 获取印章列表
    getSealList() {
      listSeal({ pageNum: 1, pageSize: 100 }).then(res => {
        if (res.code === 200) {
          this.sealList = res.rows
        }
      })
    },
    // 获取模板列表
    getTemplateList() {
      listTemplate({ pageNum: 1, pageSize: 100 }).then(res => {
        if (res.code === 200) {
          this.templateList = res.rows
        }
      })
    },

    // 选择弹窗确定按钮
    submitCreateForm() {
      createTrans({ transNos: [this.transRow.transNo], templateNo: this.chooseData.templateNo, sealNo: this.chooseData.sealNo }).then((res) => {
        if (res.code === 200) {
          this.$message.success('生成成功')
          this.chooseShow = false
          this.getList()
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    /**
     * 生成
     */
    handleCreate(row) {
      this.transRow = row
      this.chooseShow = true
    },
    /** 查询交易信息列表  */
    getList() {
      this.loading = true
      listTrans(this.queryParams).then((response) => {
        this.transList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        transNo: null,
        transDate: null,
        transAddr: null,
        transAmount: null,
        transName: null,
        transCustomerNo: null,
        transBusinessNo: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.transNo)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加交易信息'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const transNo = row.transNo || this.ids
      getTrans(transNo).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改交易信息'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.transNo != null) {
            updateTrans(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addTrans(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const transNos = row.transNo || this.ids
      this.$modal
        .confirm('是否确认删除交易信息编号为"' + transNos + '"的数据项？')
        .then(function() {
          return delTrans(transNos)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'system/trans/export',
        {
          ...this.queryParams
        },
        `trans_${new Date().getTime()}.xlsx`
      )
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = '交易信息导入'
      this.upload.open = true
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/trans/importTemplate', {
      }, `trans_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + '</div>', '导入结果', { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    }
  }
}
</script>
